home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.misc,owl.development
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: Re: Compiler Bug - can call private constructors.
- Message-ID: <marnoldDMyJCH.J7x@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <311a149f.8353477@hector> <4fnk8n$17e@daffodil.InfoChan.COM>
- Date: Sun, 18 Feb 1996 06:00:17 GMT
- Sender: marnold@netcom5.netcom.com
-
- alanj@infochan.com (Alan Johnston) writes:
-
- >Kenn@owl-uk.co.uk (Ken Nicolson) wrote:
- >>>>>> SAMPLE CODE REPRODUCING PROBLEM
-
- >>#include <stdio.h>
- >>#include <iostream.h>
-
- >>class FOOBAR
- >>{
- >>private:
- >> FOOBAR()
- >> {
- >> cout << "I've called a private constructor!\n";
- >> }
- >>};
-
-
- >>FOOBAR foobar()
- >>{
- >>// FOOBAR b; SYNTAX ERROR, as expected
- >> return FOOBAR( ); // This is wrong
- >>}
-
- >Perhaps the problem stems from the fact that 'return FOOBAR() '
- >actually calls the copy constructor. Since you haven't declared a
- >private copy constructor, the compiler is making one for you, and it
- >is public. Try declaring a private copy constructor:
- >FOOBAR(const FOOBAR&);
-
- That might "fix" things but it doesn't explain the behavior the original
- poster is getting (which is absolutely wrong).
-
- Your claim that "return FOOBAR();" causes a call to the copy ctor is true,
- but it does not *only* call the copy constructor. The regular ctor must
- be called as well because, obviously, there has to be a FOOBAR to *copy*
- in the first place. The statement "return FOOBAR();" generates a call to
- the *default* ctor, no question, and the above code should not compile
- since the default ctor is declared private.
-
- In fact, the above code does not work under my compiler, Borland C++ 4.52.
- It produces the error that "FOOBAR::FOOBAR() is not accessible", meaning
- that it can't be called. This is absolutely the correct result. Why
- the latest edition of Visual C++ has a different "opinion" is beyond me.
-
- The orignal poster's code should not compile, period. If it is "fixed"
- by creating a private copy constructor, then that only goes to show that
- something is quite wrong with the compiler he's using.
-
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-